import Vue from 'vue'
import VueRouter from 'vue-router'

import MyLogin from '@/components/MyLogin'
import MyHome from '@/components/MyHome'

import MyUsers from '@/components/menus/MyUsers'
import MySettings from '@/components/menus/MySettings'
import MyGoods from '@/components/menus/MyGoods'
import MyOrders from '@/components/menus/MyOrders'
import MyRights from '@/components/menus/MyRights'
import MyUserDetail from '@/components/user/MyUserDetail'

import pathArr from '@/router/pathArr'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: MyLogin },
    {
      path: '/home',
      component: MyHome,
      redirect: '/home/users',
      children: [
        { path: 'users', component: MyUsers },
        { path: 'rights', component: MyRights },
        { path: 'goods', component: MyGoods },
        { path: 'orders', component: MyOrders },
        { path: 'settings', component: MySettings },
        { path: 'userinfo/:id', component: MyUserDetail },

      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (pathArr.indexOf(to.path) !== -1 ) {
    const token = localStorage.getItem('token')
    if (token) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export default router